<template>
    <div style="border: 1px solid #ccc">
        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :mode="props.textEdit.mode" />
        <Editor style="height: 300px; overflow-y: hidden;" v-model="valueHtml" :defaultConfig="props.textEdit.editorConfig"
            @onChange="onChange" :mode="props.textEdit.mode" @onCreated="handleCreated" />
    </div>
</template>
<script lang="ts" setup  name="textEdit">
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
const props = defineProps<{
    textEdit: {
        mode: string,
        editorConfig: { placeholder: string },
    },
    content: string
}>()
const emit = defineEmits(['value-change'])
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()
// 内容 HTML
const valueHtml = ref<string>()
// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})
const onChange = (editor: any) => {
    emit('value-change', valueHtml.value)
}
const handleCreated = (editor: any) => {
    editorRef.value = editor // 记录 editor 实例，重要！
    valueHtml.value = props.content
}
</script>    